#!/opt/lampp/bin/perl -w
use strict;
use LWP::UserAgent;

my %urls;
$urls{'http://taobaofou.com/http_non_anonymous.html'} = 0;
$urls{'http://taobaofou.com/http_fast.html'} = 0;
$urls{'http://taobaofou.com/http_anonymous.html'} = 0;

my %proxies;
while ((my $url, my $type) = each %urls) {
	my $ua = LWP::UserAgent->new;
	$ua->agent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4)Gecko/2008111217 Fedora/3.0.4-1.fc10 Firefox/3.0.4");
	$ua->parse_head(0);
	my $request = HTTP::Request->new(GET=>$url);
	$request->header('Referer', 'http://www.51proxied.com/');
	while (1) {
		my $response = $ua->request($request);
		if ($response->is_success) {
			my $proxy_num = 0;
			my $content = $response->content;
			while ($content =~ /<td width="30">[\d]+<\/td>[\s]+<td>([\d\D]+?)<\/td>[\s]+<td width="60">([\d]+)<\/td>[\s]+<td width="60">/g) {
				$proxies{"$1:$2"} = 0;
				++$proxy_num;
			}
			print STDERR "$proxy_num proxies found in $url\n";
			if ($proxy_num == 0) {
				print STDERR $content;
			}
			else {
				last;
			}
		}
		else {
			print $response->status_line;
			print STDERR "failure\trequesting $url\n";
		}
	}
}
print STDERR "".(scalar keys %proxies)." proxies found\n";

#validate proxies
my $counter = 0;
foreach my $proxy (keys %proxies) {
	my $ua = LWP::UserAgent->new;
	$ua->agent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4)Gecko/2008111217 Fedora/3.0.4-1.fc10 Firefox/3.0.4");
	$ua->proxy('http', "http://$proxy");
	$ua->timeout(5);
	my $request = HTTP::Request->new(GET=>"http://www.baidu.com/");
	my $response = $ua->request($request);
	if ($response->is_success && index($response->content, '百度一下，你就知道')) {
		$proxies{$proxy} = 1;
		++$counter;
	}
	else {
		$proxies{$proxy} = 0;
	}
}

if ($counter > 150) {
	open OUT, ">proxy";
	while ((my $proxy, my $type) = each %proxies) {
		print OUT "$proxy\t$type\n" if ($type == 1);
	}
	close OUT;
}
else {
	print STDERR "$counter proxies only. update failed\n";
}
